﻿using H2.NET.Web.Pages.SMD.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using H2.NET.Web.Workflow.Helper;
using H2.NET.Web.API.Model;
using H2.NET.DBUtility;
using System.Data.SqlClient;


namespace H2.NET.Web.Pages.SMD
{
    public partial class uc_dealer : TaskControl
    {
        public t_smd_Dealer dealer
        {
            get
            {
                return ViewState["dealer"] == null ? new t_smd_Dealer() : (t_smd_Dealer)ViewState["dealer"];
            }
            set
            {
                ViewState["dealer"] = value;
            }
        }
        public t_smd_Store store
        {
            get
            {
                return ViewState["store"] == null ? new t_smd_Store() : (t_smd_Store)ViewState["store"];
            }
            set
            {
                ViewState["store"] = value;
            }
        }


        public string dealerId { get { return dealer_id.Value; } }
        public string code { get { return dealer_dealerCode.Text; } }
        public string name { get { return dealer_name.Text; } }
        //public string ShortName { get { return dealer_shortname.Text; } }
        public string ShortName { get { return dealer_shortname.Text; } }
        public string IsShortName
        {
            get
            {
                return ViewState["IsShortName"] == null ? "" : ViewState["IsShortName"].ToString();
            }
            set
            {
                ViewState["IsShortName"] = value;
            }
        }

        public string d_title
        {
            get
            {
                return ViewState["d_title"] == null ? "经销商信息" : ViewState["d_title"].ToString();
            }
            set
            {
                ViewState["d_title"] = value;
            }
        }
        public bool IsReadonly { get; set; }

        public bool IsCanEditAuthID
        {
            get
            {
                return ViewState["IsCanEditAuthID"] == null ? true : (bool)ViewState["IsCanEditAuthID"];
            }
            set
            {
                ViewState["IsCanEditAuthID"] = value;
            }
        }

        public bool quanxindealer
        {
            get
            {
                return ViewState["quanxindealer"] == null ? true : (bool)ViewState["quanxindealer"];
            }
            set
            {
                ViewState["quanxindealer"] = value;
            }
        }
        public bool isCanChoose
        {
            get
            {
                return ViewState["isCanChoose"] == null ? false : (bool)ViewState["isCanChoose"];
            }
            set
            {
                ViewState["isCanChoose"] = value;
            }
        }

        public bool isRegionReadonly
        {
            get
            {
                return ViewState["isRegionReadonly"] == null ? false : (bool)ViewState["isRegionReadonly"];
            }
            set
            {
                ViewState["isRegionReadonly"] = value;
            }
        }

        public bool isShowShortName
        {
            get
            {
                return ViewState["isShowShortName"] == null ? false : (bool)ViewState["isShowShortName"];
            }
            set
            {
                ViewState["isShowShortName"] = value;
            }
        }

        public bool IsCanAuthID
        {
            get
            {
                return ViewState["IsCanAuthID"] == null ? true : (bool)ViewState["IsCanAuthID"];
            }
            set
            {
                ViewState["IsCanAuthID"] = value;
            }
        }

        public bool IsSeeShortName
        {
            get
            {
                return ViewState["IsSeeShortName"] == null ? true : (bool)ViewState["IsSeeShortName"];
            }
            set
            {
                ViewState["IsSeeShortName"] = value;
            }
        }
        public bool IsShortNameHasValue
        {
            get
            {
                return ViewState["IsShortNameHasValue"] == null ? true : (bool)ViewState["IsShortNameHasValue"];
            }
            set
            {
                ViewState["IsShortNameHasValue"] = value;
            }
        }
        public bool IsStillDealer
        {
            get
            {
                return ViewState["IsStillDealer"] == null ? true : (bool)ViewState["IsStillDealer"];
            }
            set
            {
                ViewState["IsStillDealer"] = value;
            }
        }
        public void FirstShowDealer(string dealercode)
        {
            DbHelperSQL.connectionString = PubConstant.GetConnectionString("oleDB");
            //string sql = "update t_smd_Dealer set ShortName = (select top(1) Dealershortname from t_smd_SmdSyncData inner join UltimusMDC_20160726..t_StoreMasterData on t_smd_SmdSyncData.SerialNumber =UltimusMDC..t_StoreMasterData.SerialNumber  where t_smd_SmdSyncData.DealerCode =@dealercode and UltimusMDC..t_StoreMasterData.ID =(select MAX(ID) from UltimusMDC..t_StoreMasterData where DealerCode =@dealercode)) where t_smd_Dealer.DealerCode =@dealercode";
            string sql = @"update 
	                            AkzoNobelSMD..t_smd_Dealer 
                            set 
	                            ShortName = 
                            (
	                            select top(1)
		                            Dealershortname 
	                            from 
		                             AkzoNobelSMD..t_smd_SmdSyncData  as t1
	                            inner join
		                            t_StoreMasterData  as t2
	                            on 
		                            t1.SerialNumber  = t2.SerialNumber  
                                where 
		                            t1.DealerCode = @dealercode
		                            and t2.ID = (select MAX(ID) from t_StoreMasterData where DealerCode =@dealercode)
                             ) 
                              where 
	                             DealerCode = @dealercode";
            int Execute = DbHelperSQL.ExecuteSql(sql, new SqlParameter("@dealercode", dealercode));
        }
        public bool IsDMODealer
        {
            get
            {
                return ViewState["IsDMODealer"] == null ? true : (bool)ViewState["IsDMODealer"];
            }
            set
            {
                ViewState["IsDMODealer"] = value;
            }
        }
        public string guid = string.Empty;

        public string dealerCallBack { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            guid = Guid.NewGuid().ToString().Replace("-", "");
            dealer_dealerCode.Attributes.Remove("onclick");
            dealer_dealerCode.Attributes.Add("onclick", "return openDealerDialog" + guid + "(this)");
            dealer_shortname.Attributes.Remove("onclick");
            dealer_shortname.Attributes.Add("onclick", "return checkShortName" + guid + "(this)");

            D_title.Text = d_title;
            Dictionary<string, string> dic = new Dictionary<string, string>();
            dic.Add(dealer_id.ClientID, "DealerID");
            dic.Add(dealer_dealerCode.ClientID, "DealerCode");
            dic.Add(dealer_name.ClientID, "Name");
            dic.Add(dealer_region.ClientID, "Region");
            dic.Add(dealer_province.ClientID, "Province");
            dic.Add(dealer_cityName.ClientID, "CityName");
            //dic.Add(dealer_shortname.ClientID, "ShortName");
            dic.Add(dealer_shortname.ClientID, "ShortName");
            uc_search_dealer1.dic = dic;
            uc_search_dealer1.dealerCallBack = dealerCallBack;


            if (!IsPostBack)
            {
                dealer = dealer ?? new t_smd_Dealer();
                store = store ?? new t_smd_Store();//新增DealerShortName edit by Chris wang
           
                dealer_id.Value = dealer.DealerID.ToString();
                dealer_dealerCode.Text = dealer.DealerCode;
                dealer_name.Text = dealer.Name;
                //dealer_shortname.Text = dealer.ShortName;//经销商简称
                dealer_shortname.Text = store.DealerShortName;//新增DelaerShortName edit by Chris wang 
                dealer_region.Text = dealer.Region;
                dealer_province.Text = dealer.Province;
                dealer_cityName.Text = dealer.CityName;
                dealer_dealerCode_label.Visible = false;
                dealer_name_label.Visible = false;
                dealer_shortname.Visible = false;
                dealer_region_label.Visible = false;
                dealer_province_label.Visible = false;
                dealer_cityName_label.Visible = false;
                dealer_shortname_label.Visible = false;

                if (!IsCanEditAuthID)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = false;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = true;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname_label.Text = dealer.ShortName;

                }

                if (!quanxindealer)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = false;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = true;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname_label.Text = store.DealerShortName;

                }
              
                if (!IsCanAuthID)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = true;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = false;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname.Text = IsShortName;

                }
                if (!IsStillDealer)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = true;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = false;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname.Text = IsShortName;

                }
                if (!IsDMODealer)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = false;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = true;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname_label.Text = IsShortName;

                }

                if (isCanChoose)
                {
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = false;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = true;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region == null ? dealer.Region : dealer.Region.ToUpper();
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname_label.Text = IsShortName;
                }

                if (!IsCanEditAuthID && isCanChoose)
                {
                    dealer_dealerCode.Visible = true;
                    dealer_dealerCode_label.Visible = false;
                }

                if (IsReadonly)
                {
                    dealer_dealerCode.Attributes.Remove("onclick");
                    dealer_dealerCode.CssClass = "";
                }

                if (isRegionReadonly)
                {
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                }
                //Chris wang20170721新增
                if (IsSeeShortName)
                {
                     string action = TaskAPI.GetVariableBySingleValue("Actions");
                     if (action == "ChangeDealer")
                     {
                         td_name.Attributes.Add("colspan", "3");
                         td_short_name.Visible = false;
                         td_short_input.Visible = false;
                     }
                    
                }
       
                if (!IsShortNameHasValue)
                {
                    dealer_dealerCode.Visible = false;
                    dealer_name.Visible = false;
                    dealer_shortname.Visible = true;
                    dealer_region.Visible = false;
                    dealer_province.Visible = false;
                    dealer_cityName.Visible = false;
                    dealer_dealerCode_label.Visible = true;
                    dealer_name_label.Visible = true;
                    dealer_shortname_label.Visible = false;
                    dealer_region_label.Visible = true;
                    dealer_province_label.Visible = true;
                    dealer_cityName_label.Visible = true;
                    dealer_dealerCode_label.Text = dealer.DealerCode;
                    dealer_name_label.Text = dealer.Name;
                    dealer_region_label.Text = dealer.Region;
                    dealer_province_label.Text = dealer.Province;
                    dealer_cityName_label.Text = dealer.CityName;
                    dealer_shortname.Text = store.DealerShortName;

                }
                
                
              
                //if (!isShowShortName)
                //{
                //    //td_name.Attributes.Add("colspan", "3");
                //    //td_short_name.Visible = false;
                //    //td_short_input.Visible = false;
                //}
            }
        }
        /*public bool HasValue(ref string return_msg)
        {
            //bool type = false;
                if (string.IsNullOrEmpty(dealer_shortname.Text))
                {
                    return_msg="经销商简称为必填项";
                    return false;
                }
                else
                {
                    return true;
                }
        }*/
    }
}